{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "ede190e3",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import pandas as pd\n",
    "import numpy as np \n",
    "import pandas as pd\n",
    "from tqdm.notebook import tqdm\n",
    "from sklearn.model_selection import train_test_split"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "db21f14a",
   "metadata": {},
   "outputs": [],
   "source": [
    "dff = pd.read_csv('dff.csv',index_col=0)\n",
    "dff = dff.astype(float)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "9330c307",
   "metadata": {},
   "outputs": [],
   "source": [
    "def rmspe(y_true, y_pred):\n",
    "    return  (np.sqrt(np.mean(np.square((y_true - y_pred) / y_true))))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "eef86f62",
   "metadata": {},
   "outputs": [],
   "source": [
    "dff['stock_id'] = [str(x.split('_')[0]) for x in dff.index]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "d69dcc86",
   "metadata": {},
   "outputs": [],
   "source": [
    "y = dff['target']\n",
    "X = dff.drop(['target'],axis=1)\n",
    "categorical_features_indices = np.where(X.dtypes != np.float)[0]\n",
    "cat_features=categorical_features_indices\n",
    "\n",
    "X_train = X[:int(0.7*len(X))]\n",
    "y_train = y[:int(0.7*len(y))]\n",
    "X_test = X[int(0.7*len(X)):]\n",
    "y_test = y[int(0.7*len(y)):]\n",
    "#X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.8, random_state=42212)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "3226ef7b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "cd9629d1e02d4160bfc393d6d45edb88",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "MetricVisualizer(layout=Layout(align_self='stretch', height='500px'))"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from catboost import CatBoostRegressor, Pool, metrics, cv\n",
    "\n",
    "\n",
    "model = CatBoostRegressor(\n",
    "    random_seed=23333,\n",
    "    iterations = 200,\n",
    "    logging_level='Silent'\n",
    ")\n",
    "\n",
    "model.fit(\n",
    "    X_train, y_train,\n",
    "    cat_features=categorical_features_indices,\n",
    "    eval_set=(X_test, y_test),\n",
    "    plot=True\n",
    ");"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "e4ef62be",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.27523640983179143"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def rmspe(y_true, y_pred):\n",
    "    return  (np.sqrt(np.mean(np.square((y_true - y_pred) / y_true))))\n",
    "\n",
    "preds = model.predict(X_test)\n",
    "rmspe(y_test, preds)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d39f4dd2",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5e317e55",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
